package edu.msg.hrappy.backend.repository.beans;

import javax.ejb.Stateless;

import edu.msg.hrappy.backend.model.User;
import edu.msg.hrappy.backend.repository.UserRepository;
import javax.persistence.TypedQuery;
import edu.msg.hrappy.backend.repository.exception.RepositoryException;


@Stateless(name="MsguserRepository")
public class UserRepositoryBean extends BaseRepositoryBean<User, Long> implements UserRepository{

	public UserRepositoryBean() {
		super(User.class);
	}

    @Override
    public User getUserByUserName(String userName) {
        try {
            TypedQuery<User> query = (TypedQuery<User>) getManager ().createQuery (
                    "SELECT u FROM User u WHERE u.userName = :name");
            query.setParameter ("name", userName);
            
            if (query.getResultList ().isEmpty ()) {
                return null;
            } else {
                return query.getSingleResult ();
            }
        } catch (final javax.ejb.TransactionRolledbackLocalException ex) {
            throw new RepositoryException ("Get by name failed", ex);
        }
    }

}
